﻿using System;
using System.Reflection;

namespace BorgExt
{
    internal class ActivatorExtensionObjectFactory
    {
        public static object CreateExtensionObject(Extension extension, ExtensionType extensionType)
        {
            var type = extensionType.Type;

            const BindingFlags bindingAttr =
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.CreateInstance;

            var extensionArray = !extensionType.HasExtensionConstructor
                ? null
                : new[] { extension };

            object[] args = extensionArray;
            return Activator.CreateInstance(type, bindingAttr, null, args, null);
        }
    }
}

